#!/bin/bash

LOGING=0

_basename=`basename $0`
LOGF=/tmp/$_basename-$UID.log

EXIT_NOT_ROOT=1
EXIT_PARSE_OPTS=2
EXIT_NOT_EXIST=3

flags_unbind=0
mnt_origin=


#Rich Text
declare -A front_color back_color effect
front_color[black]=30
front_color[red]=31
front_color[green]=32
front_color[yellow]=33
front_color[blue]=34
front_color[magenta]=35
front_color[cyan]=36
front_color[white]=37
back_color[black]=40
back_color[red]=41
back_color[green]=42
back_color[yellow]=43
back_color[blue]=44
back_color[magenta]=45
back_color[cyan]=46
back_color[white]=47
effect[hilight]=1
effect[underline]=1
effect[shining]=5
effect[reverse]=7
effect[hide]=8
function rich_echo {
    local prefix="\033["
    local suffix="\033[0m"
    local color_effect
    local text
    while [ $# -gt 0 ]; do
        case "$1" in
        --bc)
            color_effect="$color_effect;${back_color[$2]}"
            shift;;
        --fc)
            color_effect="$color_effect;${front_color[$2]}"
            shift;;
        -r)
            color_effect="$color_effect;${front_color[red]}";;
        -g)
            color_effect="$color_effect;${front_color[green]}";;
        -s)
            color_effect="$color_effect;${effect[shining]}";;
        -h)
            color_effect="$color_effect;${effect[hilight]}";;
        -e)
            color_effect="$color_effect;${effect[$2]}"
            shift;;
        *)
            text="$1"
        esac
        shift
    done
    echo -e "$prefix${color_effect}m$text$suffix"
}


function exit_with_code {
    local code=$1
    case "$code" in
    $EXIT_NOT_ROOT)
        echo "EXIT_NOT_ROOT!";;
    $EXIT_PARSE_OPTS)
        echo "EXIT_PARSE_OPTS!";;
    $EXIT_NOT_EXIST)
        rich_echo -r -s -h "EXIT_NOT_EXIST!";;
    esac
    exit $code
}

function usage {
    echo "Usage: $0 [OPTION] <fakedir>
Options:
    -h|--help               show this help info
    -u|--unbind             unbind <fakedir>
"
}


while [ $# -gt 0 ]; do
    case "$1" in
    -u|--unbind)
        flags_unbind=1;;
    [^-]*)
        if [ -z "$mnt_origin" ]; then
            mnt_origin=$1
        else
            rich_echo --bc black --fc red -e shining  "Double mount point found: $1!" 
            exit_with_code $EXIT_PARSE_OPTS  
        fi;;
    -h|--help)
        usage
        exit 0;;
    *)
        rich_echo --bc black --fc red -e shining  "Unknow option: $1"    
        usage
        exit_with_code $EXIT_PARSE_OPTS;;
    esac
    shift
done

if [ $LOGING -eq 1 ]; then
    exec 3>&1
    exec >$LOGF 2>&1
fi

date

if [ -z "$mnt_origin" ]; then
    usage
    exit_with_code $EXIT_PARSE_OPTS
fi


if [ ! -e "$mnt_origin" ]; then
    exit_with_code $EXIT_NOT_EXIST
fi

if [ "$UID" != "0" ]; then
    exit_with_code $EXIT_NOT_ROOT
fi

mnt_basename=`basename $mnt_origin`
mnt_tmp0=/tmp/$mnt_basename-0
mnt_tmp=/tmp/$mnt_basename

if [ $flags_unbind -eq 1 ]; then
    umount $mnt_origin
    umount $mnt_tmp0
    rm $mnt_tmp0 $mnt_tmp -rf
    exit 0
fi

mkdir -p $mnt_tmp0 $mnt_tmp

mount --bind $mnt_origin $mnt_tmp0

pushd $mnt_origin >/dev/null
for node in *; do
    if [ -L "$node" ]; then
        ln -s `realpath $node` $mnt_tmp/
    else
        ln -s $mnt_tmp0/$node $mnt_tmp/
    fi
done
popd >/dev/null
mount --bind $mnt_tmp $mnt_origin
echo "$mnt_origin <==> $mnt_tmp"
